package com.faxsun.tools;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateHelper {
	private static final String FORMAT = "yyyy.MM.dd hh:mm:ss";
	private static final String INPUT_FORMAT = "MM/dd/yyyy hh:mm:ss";// 08/04/2015
																		// 00:00:00

	public static Date parse(String date, String format) {
		SimpleDateFormat sdf = getSdf(format);
		try {
			return sdf.parse(date);
		} catch (Exception e) {

		}
		return null;
	}

	public static String transform(String date, String inputForamt,
			String targetFormat) {
		if (date == null)
			return null;
		return DateHelper.format(DateHelper.parse(date,
				inputForamt == null ? INPUT_FORMAT : inputForamt),
				targetFormat == null ? FORMAT : targetFormat);
	}

	public static String format(Date date, String format) {
		SimpleDateFormat sdf = getSdf(format);
		try {
			return sdf.format(date);
		} catch (Exception e) {

		}
		return null;
	}

	private static SimpleDateFormat getSdf(String format) {
		SimpleDateFormat sdf = null;
		if (format == null) {
			sdf = new SimpleDateFormat(FORMAT);
		} else {
			sdf = new SimpleDateFormat(format);
		}
		return sdf;
	}
}
